package rdp;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.java.JavaPlugin;
import rdp.cmd.achieve.CMDAchieve;
import rdp.cmd.achieve.LSNAchieve;
import rdp.cmd.base.CMDBase;
import rdp.cmd.base.LSNBase;
import rdp.cmd.market.CMDMarket;
import rdp.cmd.market.LSNMarket;
import rdp.cmd.occupy.CMDOccupy;
import rdp.cmd.occupy.LSNOccupy;
import rdp.cmd.skill.CMDSkill;
import rdp.cmd.skill.LSNSkill;
import rdp.cmd.team.CMDTeam;
import rdp.cmd.team.LSNTeam;
import rdp.cmd.vocation.CMDVocation;
import rdp.cmd.vocation.LSNVocation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RDPMain implements CommandExecutor {
    private Map<String, Boolean> conf;

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
        boolean isAdmin = sender.hasPermission("minecraft");
        boolean isPlayer = sender instanceof Player;
        Player player = isPlayer ? (Player) sender : null;
        Player target = null;

        if (args.length == 0) {
            args = new String[]{"menu"};
        }
        if (getCmd(args[0]) == "menu") {
            if (isPlayer) {
                player.openInventory(getMenu());
                return true;
            } else
                args = new String[]{"help"};
        }
        if (getCmd(args[0]) == "help") {
            RDPUtil.SEND(sender, getHelp());
            return true;
        }
        switch (getCmd(args[0])) {
            case "achieve":
                if (conf.get("achieve"))
                    return new CMDAchieve().execute(sender, args);
                break;
            case "base":
                if (conf.get("base"))
                    return new CMDBase().execute(sender, args);
                break;
            case "market":
                if (conf.get("market"))
                    return new CMDMarket().execute(sender, args);
                break;
            case "occupy":
                if (conf.get("occupy"))
                    return new CMDOccupy().execute(sender, args);
                break;
            case "skill":
                if (conf.get("skill"))
                    return new CMDSkill().execute(sender, args);
                break;
            case "team":
                if (conf.get("team"))
                    return new CMDTeam().execute(sender, args);
                break;
            case "vocation":
                if (conf.get("vocation"))
                    return new CMDVocation().execute(sender, args);
                break;
        }
        RDPUtil.SEND(sender, "§4该指令不存在或未启用。");
        return false;
    }

    private String getCmd(String str) {
        List<String[]> list = new ArrayList();
        list.add(new String[]{"?", "help"});
        list.add(new String[]{"!", "menu"});
        list.add(new String[]{"a", "achieve"});
        list.add(new String[]{"b", "base"});
        list.add(new String[]{"c", ""});
        list.add(new String[]{"d", ""});
        list.add(new String[]{"e", ""});
        list.add(new String[]{"f", ""});
        list.add(new String[]{"g", ""});
        list.add(new String[]{"h", ""});
        list.add(new String[]{"i", ""});
        list.add(new String[]{"j", ""});
        list.add(new String[]{"k", ""});
        list.add(new String[]{"l", ""});
        list.add(new String[]{"m", "market"});
        list.add(new String[]{"n", ""});
        list.add(new String[]{"o", "occupy"});
        list.add(new String[]{"p", ""});
        list.add(new String[]{"q", ""});
        list.add(new String[]{"r", ""});
        list.add(new String[]{"s", "skill"});
        list.add(new String[]{"t", "team"});
        list.add(new String[]{"u", ""});
        list.add(new String[]{"v", "vocation"});
        list.add(new String[]{"w", ""});
        list.add(new String[]{"x", ""});
        list.add(new String[]{"y", ""});
        list.add(new String[]{"z", ""});

        for (String[] strs : list) {
            if (RDPUtil.CHECK(str, strs))
                return strs[strs.length - 1];
        }
        return "";
    }

    private String[] getHelp() {
        return new String[]{
                "§e/rdp help§f - 获取主指令组帮助。",
                "§e/rdp menu§f - 打开主菜单。",
                "§e/rdp achieve§f - 成就系统指令组。" + (conf.get("achieve") ? "§f[§a启用§f]" : "§f[§4停用§f]"),
                "§e/rdp base§f - 基础系统指令组。" + (conf.get("base") ? "§f[§a启用§f]" : "§f[§4停用§f]"),
                "§e/rdp market§f - 市场系统指令组。" + (conf.get("market") ? "§f[§a启用§f]" : "§f[§4停用§f]"),
                "§e/rdp occupy§f - 领地系统指令组。" + (conf.get("occupy") ? "§f[§a启用§f]" : "§f[§4停用§f]"),
                "§e/rdp skill§f - 技能系统指令组。" + (conf.get("skill") ? "§f[§a启用§f]" : "§f[§4停用§f]"),
                "§e/rdp team§f - 队伍系统指令组。" + (conf.get("team") ? "§f[§a启用§f]" : "§f[§4停用§f]"),
                "§e/rdp vocation§f - 职业系统指令组。" + (conf.get("vocation") ? "§f[§a启用§f]" : "§f[§4停用§f]")
        };
    }

    private Inventory getMenu() {
        Inventory inv = Bukkit.createInventory(null, 54, "主菜单");

        ItemStack blank = new ItemStack(Material.GLASS_PANE);
        ItemMeta blankIM = blank.getItemMeta();
        blankIM.setDisplayName("");
        blank.setItemMeta(blankIM);

        for (int i = 0; i < inv.getSize(); i++)
            inv.setItem(i, blank);

        Map exit = new HashMap() {{
            put("MATERIAL", Material.BARRIER);
            put("NAME", "§e帮助");
            put("LORE", new ArrayList<String>() {{
                add("§b点击§a查看帮助");
            }});
            put("LEFT", "/rdp help");
            put("RIGHT", "/rdp help");
        }};

        Map achive = new HashMap() {{
            put("MATERIAL", Material.WRITABLE_BOOK);
            put("NAME", "§e成就");
            put("LORE", new ArrayList<String>() {{
                add("§b左击§a打开菜单");
                add("§b右击§a查看帮助");
            }});
            put("LEFT", "/rdp achieve menu");
            put("RIGHT", "/rdp achieve help");
        }};

        Map base = new HashMap() {{
            put("MATERIAL", Material.NAME_TAG);
            put("NAME", "§e基础");
            put("LORE", new ArrayList<String>() {{
                add("§b左击§a打开菜单");
                add("§b右击§a查看帮助");
            }});
            put("LEFT", "/rdp base menu");
            put("RIGHT", "/rdp base help");
        }};

        Map market = new HashMap() {{
            put("MATERIAL", Material.EMERALD);
            put("NAME", "§e市场");
            put("LORE", new ArrayList<String>() {{
                add("§b左击§a打开菜单");
                add("§b右击§a查看帮助");
            }});
            put("LEFT", "/rdp market menu");
            put("RIGHT", "/rdp market help");
        }};

        Map occupy = new HashMap() {{
            put("MATERIAL", Material.SCAFFOLDING);
            put("NAME", "§e领地");
            put("LORE", new ArrayList<String>() {{
                add("§b左击§a打开菜单");
                add("§b右击§a查看帮助");
            }});
            put("LEFT", "/rdp occupy menu");
            put("RIGHT", "/rdp occupy help");
        }};

        Map skill = new HashMap() {{
            put("MATERIAL", Material.BOOK);
            put("NAME", "§e技能");
            put("LORE", new ArrayList<String>() {{
                add("§b左击§a打开菜单");
                add("§b右击§a查看帮助");
            }});
            put("LEFT", "/rdp skill menu");
            put("RIGHT", "/rdp skill help");
        }};

        Map team = new HashMap() {{
            put("MATERIAL", Material.TOTEM_OF_UNDYING);
            put("NAME", "§e团队");
            put("LORE", new ArrayList<String>() {{
                add("§b左击§a打开菜单");
                add("§b右击§a查看帮助");
            }});
            put("LEFT", "/rdp team menu");
            put("RIGHT", "/rdp team help");
        }};

        Map vocation = new HashMap() {{
            put("MATERIAL", Material.TOTEM_OF_UNDYING);
            put("NAME", "§e职业");
            put("LORE", new ArrayList<String>() {{
                add("§b左击§a打开菜单");
                add("§b右击§a查看帮助");
            }});
            put("LEFT", "/rdp vocation menu");
            put("RIGHT", "/rdp vocation help");
        }};


        if (conf.get("achieve"))
            inv.setItem(0, RDPUtil.ITEM(achive));
        if (conf.get("base"))
            inv.setItem(1, RDPUtil.ITEM(base));
        if (conf.get("market"))
            inv.setItem(12, RDPUtil.ITEM(market));
        if (conf.get("occupy"))
            inv.setItem(14, RDPUtil.ITEM(occupy));
        if (conf.get("skill"))
            inv.setItem(18, RDPUtil.ITEM(skill));
        if (conf.get("team"))
            inv.setItem(19, RDPUtil.ITEM(team));
        if (conf.get("vocation"))
            inv.setItem(21, RDPUtil.ITEM(vocation));
        inv.setItem(inv.getSize()-1, RDPUtil.ITEM(exit));

        return inv;
    }

    public RDPMain() {
    }

    public RDPMain(FileConfiguration config) {
        this.conf = new HashMap() {{
            put("achieve", config.getBoolean("achieve"));
            put("base", config.getBoolean("base"));
            put("market", config.getBoolean("market"));
            put("occupy", config.getBoolean("occupy"));
            put("skill", config.getBoolean("skill"));
            put("team", config.getBoolean("team"));
            put("vocation", config.getBoolean("vocation"));
        }};
    }

    public RDPMain(JavaPlugin plugin, FileConfiguration config) {
        this(config);
        Bukkit.getPluginManager().registerEvents(new LSNMain(), plugin);
        if (conf.get("achieve"))
            Bukkit.getPluginManager().registerEvents(new LSNAchieve(), plugin);
        if (conf.get("base"))
            Bukkit.getPluginManager().registerEvents(new LSNBase(), plugin);
        if (conf.get("market"))
            Bukkit.getPluginManager().registerEvents(new LSNMarket(), plugin);
        if (conf.get("occupy"))
            Bukkit.getPluginManager().registerEvents(new LSNOccupy(), plugin);
        if (conf.get("skill"))
            Bukkit.getPluginManager().registerEvents(new LSNSkill(), plugin);
        if (conf.get("team"))
            Bukkit.getPluginManager().registerEvents(new LSNTeam(), plugin);
        if (conf.get("vocation"))
            Bukkit.getPluginManager().registerEvents(new LSNVocation(), plugin);

    }
}
